package com.ittx.dzt.jinglyun.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PackageDS;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.ShipmentDispatchDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.jinglyun.handler.AbstractBaseJinglYunApiHandler;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.sys.response.Result;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量发货同步平台
 * <p>
 * cy.shipment.orders.dispatch
 * /open/api/delivery/notice/v2 7.2 批量发货同步平台
 * https://jlopen.jinglyun.com/main/doc?docType=2&cateId=10001&docId=20047
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/10/23
 */
@Service
public class JinglYunShipmentOrdersDispatchService extends AbstractBaseJinglYunApiHandler<ShipmentDispatchDTO> {

	@Override
	public String getJinglMethod() {
		return "/open/api/delivery/notice/v2";
	}

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SHIPMENT_ORDERS_DISPATCH;
	}

	@Override
	public Result doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDTO shipmentDispatchDTO) {
		try {
			Map<String, Object> params = new HashMap<>();
			List<Map<String, Object>> packageListMap = new ArrayList<>();
			if (CollUtil.isEmpty(shipmentDispatchDTO.getPacks())) {
				return Result.error("包裹明细不允许为空.{}", "packs is null");
			}
			if (shipmentDispatchDTO.getPacks().size() > 50) {
				return Result.error("发货包裹信息，最大数量50个");
			}
			for (PackageDS pack : shipmentDispatchDTO.getPacks()) {
				Map<String, Object> packageMap = new HashMap<>();
				if (CollUtil.isEmpty(pack.getDetails())) {
					return Result.error("出库参数不全,packs.details.sourceLineNum -> orderId,packs.details.shipQty -> itemNum 无法设值");
				}
				// orderId	Long	是	子订单ID
				packageMap.put("orderId", pack.getDetails().get(0).getSourceLineNum());
				// logisticCompany	String	是	物流公司中文名称
				packageMap.put("logisticCompany", pack.getCarrierName());
				// logisticCode	String	是	物流单号
				packageMap.put("logisticCode", pack.getWaybillCode());
				// itemNum	int	是	发货数量
				packageMap.put("itemNum", pack.getDetails().get(0).getShipQty());
				packageListMap.add(packageMap);
			}
			params.put("packageList", packageListMap);
			Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
			if (result.hasError()) {
				return result;
			}
			Map<String, Object> resultMap = (Map<String, Object>) result.getData();
			List<?> packageList = MapUtil.get(resultMap, "entry", List.class);
			return Result.success().addData(packageList);
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getJinglMethod(), e.getMessage());
			ExceptionLog.error(e, PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getJinglMethod(), e.getMessage());
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(msg);
		}
	}
}
